local jiushi = fk.CreateSkill {

  name = "joy__jiushi",

  tags = {  },

}



jiushi:addEffect("viewas", {
  name = "joy__jiushi",
  anim_type = "support",
  prompt = "#joy__jiushi-active",
  pattern = "analeptic",
  card_filter = function() return false end,
  before_use = function(self, player)
    player:turnOver()
  end,
  view_as = function(self, player,cards)
    local c = Fk:cloneCard("analeptic")
    c.skillName = jiushi.name
    return c
  end,
  enabled_at_play = function (self, player)
    return player.faceup
  end,
  enabled_at_response = function (self, player)
    return player.faceup
  end,
})

jiushi:addEffect(fk.Damaged, {
  name = "#joy__jiushi_trigger",
  anim_type = "support",
  mute = true,
  events = {fk.Damaged, fk.TurnedOver},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(jiushi.name) then
      if event == fk.Damaged then
        return not player.faceup and data.jiushi
      elseif event == fk.TurnedOver then
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return event == fk.TurnedOver or player.room:askForSkillInvoke(player, jiushi.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, jiushi.name)
    player:broadcastSkillInvoke(jiushi.name)
    if event == fk.Damaged then
      player:turnOver()
    elseif event == fk.TurnedOver and not player.dead then
      local cards = player.room:getCardsFromPileByRule(".|.|.|.|.|trick")
      if #cards > 0 then
        room:obtainCard(player, cards[1], true, fk.ReasonJustMove)
      end
    end
  end,
})
jiushi:addEffect(fk.TurnedOver, {
  name = "#joy__jiushi_trigger",
  anim_type = "support",
  mute = true,
  events = {fk.Damaged, fk.TurnedOver},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(jiushi.name) then
      if event == fk.Damaged then
        return not player.faceup and data.jiushi
      elseif event == fk.TurnedOver then
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return event == fk.TurnedOver or player.room:askForSkillInvoke(player, jiushi.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, jiushi.name)
    player:broadcastSkillInvoke(jiushi.name)
    if event == fk.Damaged then
      player:turnOver()
    elseif event == fk.TurnedOver and not player.dead then
      local cards = player.room:getCardsFromPileByRule(".|.|.|.|.|trick")
      if #cards > 0 then
        room:obtainCard(player, cards[1], true, fk.ReasonJustMove)
      end
    end
  end,
})
jiushi:addEffect(fk.DamageInflicted, {
  refresh_events = {fk.DamageInflicted},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(jiushi.name) and not player.faceup
  end,
  on_refresh = function(self, event, target, player, data)
    data.jiushi = true
  end,
})

return jiushi